package xuan.gui;

import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

import java.awt.BorderLayout;

import javax.swing.JButton;
import java.awt.Dimension;
import javax.swing.JPanel;
import javax.swing.JTextField;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;


/**
 * class representing Open File Window used to select files from system
 * 
 * @author xuan
 */
@SuppressWarnings("serial")
public class OpenFileWindow extends JDialog implements ActionListener{
	private JPanel panel;
	private JTextField txtBaseFile;
	private JTextField txtBranch1;
	private JTextField txtBranch2;
	private JButton btnBaseFile;
	private JButton btnBranch1;
	private JButton btnBranch2;
	private JButton btnOk;
	private JButton btnCancel;
	private boolean okPressed;//ensure that user enter all three needed files
	
	public OpenFileWindow(JFrame parent) {
		super(parent, "Connect", true);
		setResizable(false);
		setMaximumSize(new Dimension(500, 170));
		
		setMinimumSize(new Dimension(500, 170));
		setTitle("Open");
		setPreferredSize(new Dimension(500, 170));
		
		panel = new JPanel();
		getContentPane().add(panel, BorderLayout.CENTER);
		
		JLabel lblBaseFile = new JLabel("Base File   ");
		panel.add(lblBaseFile);		
		txtBaseFile = new JTextField();
		txtBaseFile.setToolTipText("");
		panel.add(txtBaseFile);
		txtBaseFile.setColumns(25);		
		btnBaseFile = new JButton("Browser");
		btnBaseFile.addActionListener(this);
		panel.add(btnBaseFile);
		
		JLabel lblBranch1 = new JLabel("Branch 1    ");
		panel.add(lblBranch1);	
		txtBranch1 = new JTextField();
		txtBranch1.setToolTipText("");
		txtBranch1.setColumns(25);
		panel.add(txtBranch1);
		btnBranch1 = new JButton("Browser");
		btnBranch1.addActionListener(this);
		panel.add(btnBranch1);
		
		JLabel lblBranch2 = new JLabel("Branch 2    ");
		panel.add(lblBranch2);	
		txtBranch2 = new JTextField();
		txtBranch2.setToolTipText("");
		txtBranch2.setColumns(25);
		panel.add(txtBranch2);
		btnBranch2 = new JButton("Browser");	
		btnBranch2.addActionListener(this);
		panel.add(btnBranch2);
		
		btnOk = new JButton("OK");
		btnOk.setPreferredSize(new Dimension(81, 25));
		btnOk.addActionListener(this);
		panel.add(btnOk);
		
		btnCancel = new JButton("Cancel");
		btnCancel.addActionListener(this);
		panel.add(btnCancel);
		
		setLocationRelativeTo(null);
	}
	

	@Override
	public void actionPerformed(ActionEvent evt) {
		Object button = evt.getSource();
		if (button == btnOk) {
			if(!txtBaseFile.getText().isEmpty() && !txtBranch1.getText().isEmpty() && !txtBranch2.getText().isEmpty()){
				okPressed = true;
			    setVisible(false);
			}
			else{
				JOptionPane.showConfirmDialog(panel,"files missed !!!","",JOptionPane.CLOSED_OPTION);
			}
		}
		if (button == btnCancel){
			setVisible(false);
		}
		if(button==btnBaseFile){
			try{
				JFileChooser chooser = new JFileChooser(new File(new File(".").getCanonicalPath()));
				chooser.showOpenDialog(panel);
				txtBaseFile.setText(chooser.getSelectedFile().getAbsolutePath());
			}
			catch(Exception e){}
    	}
    	if(button==btnBranch1){
			try{
	    		JFileChooser chooser = new JFileChooser(new File(new File(".").getCanonicalPath()));
				chooser.showOpenDialog(panel);
				txtBranch1.setText(chooser.getSelectedFile().getAbsolutePath());
			}
			catch(Exception e){}
    	}
    	if(button==btnBranch2){
			try{
	    		JFileChooser chooser = new JFileChooser(new File(new File(".").getCanonicalPath()));
				chooser.showOpenDialog(panel);
				txtBranch2.setText(chooser.getSelectedFile().getAbsolutePath());
			}
			catch(Exception e){}
    	}
	}
	
	
	public boolean showDialog(Transfer transfer) {		  	
		  	okPressed = false;
		  	show();
		    if (okPressed) {
		    	transfer.base = txtBaseFile.getText();
		    	transfer.t1 = txtBranch1.getText();
		    	transfer.t2 = txtBranch2.getText();
		    }
		    
		    return okPressed;
	  }
}
